import { _decorator, Animation, Component, EPhysics2DDrawFlags, EventKeyboard, Input, input, KeyCode, Node, PhysicsSystem2D, RigidBody2D, v2, Vec2 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('charactersCtrl')
export class charactersCtrl extends Component {

    private roleAnim: Animation;
    private roleNode: Node;
    private direction: Vec2 = v2(0, 0);

    private rigidbody: RigidBody2D;

    private runSpeed: Vec2 = v2(6, 0);


    start() {
        this.openDebugInfo();
        this.find();
        this.addTouchEvent();
    }

    openDebugInfo() {
        // PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Aabb |
        //     EPhysics2DDrawFlags.Pair |
        //     EPhysics2DDrawFlags.CenterOfMass |
        //     EPhysics2DDrawFlags.Joint |
        //     EPhysics2DDrawFlags.Shape;

        // this.scheduleOnce(() => {
            PhysicsSystem2D.instance.debugDrawFlags =  EPhysics2DDrawFlags.Shape;
        // }, 10)
    }

    lateUpdate(deltaTime: number) {

        if (this.direction.x != 0) {
            this.rigidbody.linearVelocity = v2(this.runSpeed.x * this.direction.x, this.rigidbody.linearVelocity.y);
            console.log("移动", this.rigidbody.linearVelocity);
        } else {
            this.rigidbody.linearVelocity = v2(0, this.rigidbody.linearVelocity.y);
        }
    }


    playAnim(anim: CharactersAnimation) {
        if (this.direction.x != 0) {
            const scaleDir = this.direction.x > 0 ? 1 : -1;
            this.roleNode.setScale(scaleDir, this.roleNode.scale.y);
        }
        this.roleAnim.play(anim.toString());
    }

    find() {
        this.roleAnim = this.node.getComponent(Animation);
        this.roleNode = this.node.getChildByName("role");
        this.rigidbody = this.node.getComponent(RigidBody2D);
    }

    addTouchEvent() {
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
    }

    onKeyDown(event: EventKeyboard) {
        switch (event.keyCode) {
            // case KeyCode.KEY_W:
            // case KeyCode.ARROW_UP:
            //     this.direction.y = 1;
            //     console.log('Press w|up key');
            //     break;

            // case KeyCode.KEY_S:
            // case KeyCode.ARROW_DOWN:
            //     this.direction.y = -1;
            //     console.log('Press s|down key');
            //     break;

            case KeyCode.KEY_A:
            case KeyCode.ARROW_LEFT:
                this.direction.x = -1;
                this.playAnim(CharactersAnimation.run);
                console.log('Press a|left key');
                break;

            case KeyCode.KEY_D:
            case KeyCode.ARROW_RIGHT:
                this.direction.x = 1;
                this.playAnim(CharactersAnimation.run);
                console.log('Press d|right key');
                break;


            case KeyCode.SPACE:
                this.direction.y = 1;
                this.jump();
                console.log('Press SPACE key');
                break;
        }
        console.log("onKeyDown", this.direction);
    }

    onKeyUp(event: EventKeyboard) {
        switch (event.keyCode) {
            // case KeyCode.KEY_W:
            // case KeyCode.ARROW_UP:
            //     this.direction.y = 0;
            //     console.log('Press w|up key');
            //     break;

            // case KeyCode.KEY_S:
            // case KeyCode.ARROW_DOWN:
            //     this.direction.y = 0;
            //     console.log('Press s|down key');
            //     break;

            case KeyCode.KEY_A:
            case KeyCode.ARROW_LEFT:
                this.direction.x = 0;
                this.playAnim(CharactersAnimation.idle);
                console.log('Press a|left key');
                break;

            case KeyCode.KEY_D:
            case KeyCode.ARROW_RIGHT:
                this.direction.x = 0;
                this.playAnim(CharactersAnimation.idle);
                console.log('Press d|right key');
                break;
        }
        console.log("onKeyUp", this.direction);
    }


    jump() {
        this.rigidbody.linearVelocity = v2(this.rigidbody.linearVelocity.x, 15);
    }
}

enum CharactersAnimation {
    /**
     * 二段跳
     */
    doubleJump = "doubleJump",

    /**
     * 下落
     */
    fall = "fall",

    /**
     * 待机
     */
    idle = "idle",

    /**
     * 跳跃
     */
    jump = "jump",

    /**
     * 奔跑
     */
    run = "run",

    /**
     * 抓墙
     */
    wallJump = "wallJump",
}
